#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include "errsys.h"

void insertion_sort(int *vec, int size)
{
	int i,j,t;
	for(i=2;i<size;++i)
	{
		t=vec[i];
		for(j=i-1;j>=0;--j)
		{
			if(vec[j]>=t)
			{
				vec[j+1]=vec[j];
				if(j==0)
					vec[0]=t;
			}else{
				vec[j+1]=t;
				break;
			}
		}
	}
}

int main(int argc, char **argv)
{
	if(argc==1)
	{
		printf("Usage:%s interger\n",argv[0]);
		exit(1);
	}

	int size=atoi(argv[1]);
	int *vec = (int*)malloc(size*sizeof(int));
	srand(time(0));
	int i;
	for(i=0;i<size;++i)
		vec[i]=rand()%size;
	clock_t time = clock();
	insertion_sort(vec,size);
	printf("Time:%d\n",clock()-time);

	for(i=0;i<size;++i)
	{
		if(i%6==0)
			printf("\n");
		printf("%9d  ",vec[i]);
	}
	
	printf("\n");
	free(vec);

	return 0;
}

